package com.phone.servlet.user;

import com.phone.DBHelper;
import com.phone.handle.UserHandle;
import com.phone.servlet.BaseServlet;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by tisen on 2017/7/19.
 */
public class Check extends BaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String phone = request.getParameter("phone");
        Session session = DBHelper.getSession();
        session.beginTransaction();
        String tip = null;
        UserHandle handle = new UserHandle(session);
        try {
            tip = handle.checkPhone(phone) ? "true" : "false";
            session.getTransaction().commit();
        } catch (HibernateException exception) {
            exception.fillInStackTrace();
            tip = "error";
            session.getTransaction().rollback();
        } catch (Exception e) {
            tip = "error";
            e.printStackTrace();
        } finally {
            if (tip == null) {
                tip = "complete";
            }
            if (session != null && session.isOpen())
                session.close();
            out(response, tip);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
